# -*- coding: utf-8 -*-
import sys

from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class WidgetLED(QLabel):
    LED_ENABLE = 1
    LED_DISABLE = 2
    LED_WARNNING = 3

    def __init__(self, *args, **kwargs):
        text = kwargs.pop("text", "")
        super(WidgetLED, self).__init__(*args, **kwargs)
        self.InitUI()

    def InitUI(self):
        # 背景颜色设置
        self.bgColor = QColor(0, 0, 0, 0)
        self.enableColor = QColor(0, 255, 0)
        self.disableColor = QColor(166, 166, 166)
        self.warnColor = QColor(255, 0, 0)
        self.currentColor = self.disableColor

    # 设置取值接口
    def setStatus(self, status):
        """
            设置LED灯的颜色

        :param status: 1-使能（绿色） 2-失效（灰色） 3-告警（红色）
        :return:
        """
        if status == self.LED_ENABLE:
            self.currentColor = self.enableColor
        elif status == self.LED_WARNNING:
            self.currentColor = self.warnColor
        else:
            self.currentColor = self.disableColor
        self.update()

    def paintEvent(self, event):
        width = self.width()
        height = self.height()

        painter = QPainter(self)  # 初始化painter
        # painter.translate(width / 2, height / 2)  # 坐标轴变换，调用translate()将坐标原点平移至窗口中心

        # 坐标刻度自适应
        # side = min(width, height)
        # painter.scale(side / 200.0, side / 200.0)
        self.bgWidth = width
        self.bgHeight = height

        # 启用反锯齿，使画出的曲线更平滑
        painter.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)
        painter.begin(self)
        # 开始画图
        painter.setPen(Qt.NoPen)
        brush = QBrush(Qt.SolidPattern)
        brush.setColor(self.currentColor)
        painter.setBrush(brush)
        rect = QRectF(0, 0, self.bgWidth, self.bgHeight)
        painter.drawEllipse(rect)

        painter.end()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = WidgetLED()
    # demo.InitUI()
    demo.show()
    demo.setGeometry(100, 100, 200, 200)
    demo.setStatus(demo.LED_WARNNING)
    # demo.setValue(80)  # 设置指针指向
    # demo.setTitle("Title_____")  # 设置图标题
    sys.exit(app.exec_())
